package com.firemoss.atonement.entitymanager.mapper
{
	import com.firemoss.atonement.mapper.Mapping;
	import com.firemoss.atonement.mapper.resolution.CollectionValueResolverHelper;
	import com.firemoss.atonement.mapper.resolution.IValueResolver;
	import com.firemoss.atonement.metadata.Property;
	import flash.utils.Dictionary;

	public class EntityValueResolver implements IValueResolver
	{

		//----------------------------------------------------------
		//   Constructor 
		//----------------------------------------------------------

		public function EntityValueResolver()
		{
		}

		//----------------------------------------------------------
		// Public Methods 
		//----------------------------------------------------------

		public function resolve( source : *, targetProperty : Property, mapping : Mapping, mappingChain : Dictionary ) : *
		{
			if ( source == null )
			{
				return null
			}
			else if ( mappingChain[ source ] == null )
			{
				mappingChain[ source ] = source

				return CollectionValueResolverHelper.createCollectionItem( source, source, targetProperty, mapping, mappingChain, targetProperty.type.clazz )
			}
			else
			{
				return mappingChain[ source ]
			}
		}
	}
}
